package com.example.maybeaccount.Entry;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;

import com.example.maybeaccount.MainActivity;
import com.example.maybeaccount.Mode.Account;
import com.example.maybeaccount.Mode.AsteriskPasswordTrans;
import com.example.maybeaccount.Mode.Check_ok;
import com.example.maybeaccount.Mode.EncrypDES;
import com.example.maybeaccount.Mode.SqliteDB;
import com.example.maybeaccount.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author whb
 * 注册
 */
public class Register extends AppCompatActivity {
    private Button reg;
    private Button login;
    private EditText count;
    private EditText pwd;
    private EditText editTextPwdCheck;
    private TextView state;
    private List<User> userList;
    private List<User> dataList = new ArrayList<>();
    private Check_ok checkOk = new Check_ok();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        reg= (Button) findViewById(R.id.regin);
        count= (EditText) findViewById(R.id.count);
        pwd= (EditText) findViewById(R.id.pwd);
        editTextPwdCheck = findViewById(R.id.pwd_check);
        pwd.setTransformationMethod(new AsteriskPasswordTrans());
        editTextPwdCheck.setTransformationMethod(new AsteriskPasswordTrans());
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    EncrypDES encrypDES = new EncrypDES();
                    String name= count.getText().toString();
                    String pass= pwd.getText().toString();
                    String pswdCheck = editTextPwdCheck.getText().toString();
                    if(name == null || pass == null || pswdCheck == null){
                        Toast.makeText(getApplicationContext(),"输入不能为空",Toast.LENGTH_LONG).show();
                        return;
                    }
                    if( !pswdCheck.equals(pass)){
                        Toast.makeText(getApplicationContext(),"两次密码输入不一致",Toast.LENGTH_LONG).show();
                        editTextPwdCheck.setText("");
                        return;
                    }
                    User user=new User();
                    if(checkOk.password_limit(pass)){
                        name = encrypDES.encrypt(name);
                        pass = encrypDES.double_encrypt(pass);
                        user.setUsername(name);
                        user.setUserpwd(pass);
                        user.setGridPwd("00000");
                        Toast.makeText(getApplicationContext(),"文本密码设置成功",Toast.LENGTH_LONG).show();
                        int result= SqliteDB.getInstance(getApplicationContext()).saveUser(user);
                        if (result==1) {
                            //自动生成默认账户
                            Account alipay = new Account("支付宝", "移动支付", 0.0, "无");
                            Account wechat = new Account("微信支付", "移动支付", 0.0, "无");
                            Account pingan = new Account("平安银行卡", "银行卡", 0.0, "无");
                            result = SqliteDB.getInstance(getApplicationContext()).saveAccount(alipay);
                            result *= SqliteDB.getInstance(getApplicationContext()).saveAccount(wechat);
                            result *= SqliteDB.getInstance(getApplicationContext()).saveAccount(pingan);
                            if(result == 1){
                                Intent intent = new Intent(Register.this, Entrance.class);
                                startActivity(intent);
                            }else{
                                Toast.makeText(getApplicationContext(),"注册失败！请重试",Toast.LENGTH_LONG).show();

                            }
                        }else{
                            Toast.makeText(getApplicationContext(),"注册失败！请重试",Toast.LENGTH_LONG).show();
                        }
                        /*
                        Intent intent = new Intent(Register.this, NineGridRegister.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("user",user);
                        intent.putExtras(bundle);
                        startActivity(intent);*/
                    }
                    else{
                        Toast.makeText(getApplicationContext(),"密码设置不合法",Toast.LENGTH_LONG).show();
                        pwd.setText("");
                    }
                }catch (Exception e){
                    Log.d("错误",e.getMessage());
                }

            }
        });
    }
}
